package com.llamarun;

import java.util.ArrayList;
import java.util.Random;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.util.Log;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
 
public class GameView extends SurfaceView implements SurfaceHolder.Callback 
{
	private static final String TAG = GameView.class.getSimpleName();
 
	private MainThread thread;
	static Random randy;
	private Llama tmpLlama;
    private int testInt = 0;
    private int gameSpeed = 10;
    private int speedUpFactor = 1;
    // right to left scroll tracker for near and far BG
    private int mBGFarMoveX = 0;
    private GameObjectFactory objectFactory;
    private ArrayList<GameObject> gameObjects;
    
	public GameView(Context context) 
	{
		super(context);
		getHolder().addCallback(this);
 
		gameObjects = new ArrayList<GameObject>();
		gameObjects.add(new Llama(this));
		objectFactory = new GameObjectFactory(this);
        
		
	  	// create the game loop thread
		thread = new MainThread(getHolder(), this);
		// initialize the random number generator
		randy = new Random();
		
		setFocusable(true);
	}
 
	@Override
	public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) 
	{
		
	}
 
	@Override
	public void surfaceCreated(SurfaceHolder holder) 
	{
		Log.d(TAG, "surfaceCreated");
		thread.setRunning(true);
		thread.start();
	}
 
	@Override
	public void surfaceDestroyed(SurfaceHolder holder) 
	{
		boolean retry = true;
		while (retry) {
			try {
				thread.join();
				retry = false;
			} catch (InterruptedException e) {
				// try again shutting down the thread
			}
		}
	}
 
	@Override
	public boolean onTouchEvent(MotionEvent event) 
	{
		return super.onTouchEvent(event);
	}
 
	@Override
	protected void onDraw(Canvas canvas)
	{
		if(canvas != null)
		{
			/*int color = randy.nextInt(4);
			switch(color)
			{
			case 0:
				canvas.drawColor(Color.GREEN);
				break;
			case 1:
				canvas.drawColor(Color.BLUE);
				break;
			case 2:
				canvas.drawColor(Color.RED);
				break;
			case 3:
				canvas.drawColor(Color.GRAY);
				break;
			default:
			
			}*/
			
			Bitmap bMap = (Bitmap) BitmapFactory.decodeResource(getResources(), R.drawable.background);
			
			//Scrolling background is working
			//see: http://developer.android.com/resources/samples/JetBoy/src/com/example/android/jetboy/JetBoyView.html
			
			// decrement the far background
            mBGFarMoveX = mBGFarMoveX - gameSpeed;

            // calculate the wrap factor for matching image draw
            int newFarX = bMap.getWidth() - (-mBGFarMoveX);

            // if we have scrolled all the way, reset to start
            if (newFarX <= 0) {
                mBGFarMoveX = 0;
                // only need one draw
                canvas.drawBitmap(bMap, mBGFarMoveX, 0, null);
            } 
            else {
                // need to draw original and wrap
                canvas.drawBitmap(bMap, mBGFarMoveX, 0, null);
                canvas.drawBitmap(bMap, newFarX, 0, null);
            }
            
            GameObject newObject = objectFactory.createGameObject();
            if (newObject != null)
            {
        		Random generator = new Random();
        		newObject.setX(this.getWidth());
        		newObject.setY(generator.nextInt(this.getHeight()));
            	gameObjects.add(newObject);
            }
            
            for (GameObject o : gameObjects)
            {
            	o.draw(canvas);
            	o.update();
            }
		
		}
	}
}
